package com.jeasonzhao.commons.chs;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.ResourceException;
import com.jeasonzhao.commons.xml.XMLException;
import com.jeasonzhao.commons.xml.XMLHelper;
import com.jeasonzhao.commons.xml.XMLNode;

public class RegionHelper
{
    private static final java.util.ArrayList<Region> regions = new java.util.ArrayList<Region>();
    static
    {
        try
        {
            for(XMLNode node : XMLHelper.myConfiguration(RegionHelper.class).selectNodes("items/region"))
            {
                Region r = new Region(node.valueOf("parentCode")
                                      ,node.valueOf("code")
                                      ,node.valueOf("name")
                                      ,node.valueOf("fullName")
                                      ,Algorithms.enumFrom(RegionType.class,node.valueOf("type"))
                                      ,Algorithms.enumFrom(RegionDetailedType.class,node.valueOf("detailedType"))
                                      ,node.valueOf("toll")
                                      ,node.valueOf("zipcode")
                    );
                regions.add(r);
//                System.out.println(r+" "+regions.size());
            }
        }
        catch(ResourceException ex)
        {
            ex.printStackTrace();
        }
        catch(XMLException ex)
        {
            ex.printStackTrace();
        }
    }

    private RegionHelper()
    {

    }

    public static final Region get(String code)
    {
        if(null == code || code.trim().length() < 1)
        {
            return null;
        }
        code = code.trim();
        while(code.length() < 6)
        {
            code += "0";
        }
        for(Region r : regions)
        {
            if(r.getCode().equals(code))
            {
                return r;
            }
        }
        return null;
    }

    public static final java.util.ArrayList<Region> getSubRegions(String code)
    {
        if(null == code || code.trim().length() < 1)
        {
            code = Region.ROOT;
        }
        code = code.trim();
        while(code.length() < 6)
        {
            code += "0";
        }

        java.util.ArrayList<Region> ret = new java.util.ArrayList<Region>();
        if(code.equalsIgnoreCase(Region.ROOT) == false && code.endsWith("00") == false)
        {
            return ret;
        }
        for(Region r : regions)
        {
            if(code.equalsIgnoreCase(r.getParentCode()))
            {
                ret.add(r);
            }
        }
        return ret;
    }
    public static final java.util.ArrayList<Region> getAllSubRegions(String code,boolean includeme)
        {
            if(null == code || code.trim().length() < 1)
            {
                code = "";
            }
            code = code.trim();
            while(code.endsWith("0"))
            {
                code=code.substring(0,code.length()-1);
            }
            java.util.ArrayList<Region> ret = new java.util.ArrayList<Region>();
            if(null == code || code.trim().length() < 1||Region.ROOT.equals(code))
            {
                ret.addAll(regions);
                return ret;
            }
            else if(code.length()>=6)
            {
                if(includeme)
                {
                    Region r=get(code);
                    if(null!=r)
                    {
                        ret.add(r);
                    }
                }
                return ret;
            }
            String mycode = code;
            if(includeme==false)
            {
                while(mycode.length() < 6)
                {
                    mycode += "0";
                }
            }
            else
            {
                mycode="XX";
            }
            for(Region r : regions)
            {
                if(r.getCode().startsWith(code) && r.getCode().equals(mycode)==false)
                {
                    ret.add(r);
                }
            }
            return ret;
    }
    public static final java.util.ArrayList<Region> getRegions()
    {
        java.util.ArrayList<Region> ret = new java.util.ArrayList<Region>();
        for(Region r : regions)
        {
//            if(r.getDetailedType() == RegionDetailedType.District
//               || r.getDetailedType() == RegionDetailedType.MunicipalDistrict
//               || r.getDetailedType() == RegionDetailedType.MunicipalCountry
//               || r.getDetailedType() == RegionDetailedType.MunicipalCountrySummary
//                )
//            {
//                continue;
//            }
            ret.add(r);
        }
        return ret;
    }

    public static void main(String[] argvs)
    {
        java.util.ArrayList<Region> ret = RegionHelper.getSubRegions("51");
        for(Region r : ret)
        {
            System.out.println(r.getFullName() + " " + r.getCode() + " " + r.getType() + "  " + r.getDetailedType());
        }
    }
}
